#include "MainGame.h"
#include "ShareComponents.h"
#include "RSManager.h"
#include "QuadTree.h"
#include "Box.h"
#include "FWGame.h"
#include "FWKeyboard.h"
#include "FWGraphic.h"
#include "FWContentManager.h"
#include "Map.h"
#include "Camera.h"

#include "Object.h"
#include "IS.h"
#include "FWTexture.h"
#include <list>
#include "StaticObject.h"
#include "Player.h"
#include "Bridge.h"
#include "BulletManager.h"
#include "Bullet.h"

#include "DefenseCannon.h"

#include "Sniper.h"
#include "StandSniper.h"
#include "Cannon.h"
#include "Boss.h"
#include "BackGround.h"


MainGame::MainGame(HINSTANCE hIns,char* windowName,int w,int h):FWGame(hIns,windowName,w,h)
{
	D3DXMatrixIdentity(&this->matrix_transform);
}

MainGame::~MainGame(void)
{
}
void MainGame::load()
{	

	this->keyboard = new FWKeyboard(this);	
	if(!keyboard->init()) MessageBox(NULL,"Error keyboard!","Error!",MB_OK);		
	gl_keyboard = this->keyboard;

	this->graphic = new FWGraphic(this);		
	graphic->init();	
	gl_graphic = graphic;	
	
	content  = new FWContentManager(this);
	gl_content = content;	
	RSManager::getInstance()->Init();
	this->_map = new Map();
	this->_map->init(MAP1);
	this->_map->readMap("Resources\\MAP\\map1.txt");
	gl_map =  _map;
	_bg = new BackGround(1);
	
	_quadTree = new QuadTree(new Box(D3DXVECTOR2(0,0),D3DXVECTOR2(_map->getWidth() * 32, _map->getHeight() * 32),D3DXVECTOR2(0, 0)));
	
	_cam = new Camera(0, _map->getWidth() * 32, _map->getHeight() * 32);
	
	gl_camera = _cam;
	test = gl_content->loadTexture("gr.png");
	grounds = _map->getStaticObjects();
	globe_grounds = grounds;
	for (StaticObject* i : grounds)
	{
		_quadTree->insert(i);
	}
	gl_Quadtree = _quadTree;

	loadEnemy();
	Box* player_pos = new Box(D3DXVECTOR2(6400, 400), D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0));		
	_player = new Player(player_pos);	
	_player->init();
	
	this->_bridge = _map->getStaticBridge();
	for (Bridge* i : _bridge)
	{
		_quadTree->insert(i);
	}
	
	BulletManager::create();


	_lDefenseCannon = _map->getDefenseCanon();
	int i = 0;
	for (DefenseCannon* d : _lDefenseCannon)
	{
		d->init();
		d->setIndexOfSprite(i++);
	}	
}

void MainGame::render()
{	
	graphic->clear(D3DCOLOR_XRGB(255,255,255));
	graphic->begin(_cam->getTransformedMatrix());	
	
	_map->render();
	_bg->render();
	RenderDebug();
	
	renderEnemies();
	_player->render();
	for (Bridge* i : _bridge)
	{
		i->render();
	}	

	list<Bullet*> bbbbb =  BulletManager::getInstance()->getUsingList();
	for (Bullet* b : bbbbb)
	{
		b->render();
	}

	for (DefenseCannon* d : _lDefenseCannon)
	{
		d->render();
	}

	graphic->end();
}

void MainGame::shutdown()
{

}

void MainGame::update(float time)
{		
	keyboard->getState();		

	_bg->update();

	is->update();
	_player->update();
	_cam->update(_player->getBox()->_position);
	for (Bridge* i : _bridge)
	{
		i->update();
	}	
	list<Bullet*> bbbbb =  BulletManager::getInstance()->getUsingList();
	for (Bullet* b : bbbbb)
	{
		if(b->hasLife())
		{
			BulletManager::getInstance()->collect(b);
		}
		
		b->update();

	}
	
	for (DefenseCannon* d : _lDefenseCannon)
	{		
		d->update();		
	}
	 	
	for (Sniper* i :_lSniper)
	{
		i->update();
	}
	for (StandSniper* i :_lStandSniper)
	{
		i->update();
	}
	for (WallTurret* i :_lWallTurret)
	{
		i->update();
	}
	for (Cannon* i :_lCannon)
	{
		i->update();
	}
	for(Boss* b : _lBoss)
	{
		b->update();
	}

	keyboard->saveState();
}

void MainGame::RenderDebug()
{
	for(StaticObject* i : grounds)
	{
		graphic->tDraw(test,i->getBox()->getRect(),i->getBox()->getCenter(),D3DCOLOR_XRGB(5,25,55));
	}
	//graphic->tDraw(test,gl_ground_player->getRect(),gl_ground_player->getCenter(),D3DCOLOR_XRGB(5,255,55));

	if (gl_predict)
	{
		graphic->tDraw(test,gl_predict->getRect(),gl_predict->getCenter(),D3DCOLOR_XRGB(0,0,255));
	}
	
	//graphic->tDraw(test,gl_player->getRect(),gl_player->getCenter(),D3DCOLOR_XRGB(255,255,255));
	if(gl_ground != NULL)
		graphic->tDraw(test,gl_ground->getRect(),gl_ground->getCenter(),D3DCOLOR_XRGB(255,0,0));
	graphic->tDraw(test,gl_move->getRect(),gl_move->getCenter(),D3DCOLOR_XRGB(0,0,0));
	//if(gl_fall!= NULL)
		//graphic->tDraw(test,gl_fall->getRect(),gl_fall->getCenter(),D3DCOLOR_XRGB(100,100,100));

}

void MainGame::loadEnemy()
{
	Box* is_pos = new Box(D3DXVECTOR2(90, 300), D3DXVECTOR2(0, 0), D3DXVECTOR2(2.4f, 0));
	is = new IS(is_pos);
	is->init();
	_lSniper = _map->getSniperList();
	_lStandSniper = _map->getStandSniperList();
	_lWallTurret = _map->getWallturretList();
	_lCannon = _map->getCannonList();
	_lBoss = _map->getBoss();
}

void MainGame::renderEnemies()
{
	is->render();
	for (Sniper* i :_lSniper)
	{
		i->render();
	}
	for (StandSniper* i :_lStandSniper)
	{
		i->render();
	}
	
	for (WallTurret* i :_lWallTurret)
	{
		i->render();
	}
	for (Cannon* i :_lCannon)
	{
		i->render();
	}
	for(Boss* b : _lBoss)
	{
		b->render();
	}
}
